﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 身份证校验
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 校验身份证号
        /// 
        /// <param name="id"></param>
        /// <returns></returns>
        private bool CheckCardId(string id)
        {
            int[] wQuan = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
            string yanzhen = "10X98765432";
            string number18 = id.Substring(17);
            int sum = 0;

            for (int i = 0; i < 17; i++)
            {
                sum = sum + (Convert.ToInt32(id[i].ToString()) * wQuan[i]);
            }

            int mod = sum % 11;
            string result = yanzhen[mod].ToString();
            if (number18.Equals(result,StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
                
            return false;
        }

        private void txtShengFengZheng_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void butYanZheng_Click(object sender, EventArgs e)
        {
            int age = 0;
            string id = txtShengFengZheng.Text.Trim();
            int year = 0;

            if (id.Length == 15)
            {
                year = Convert.ToInt32(id.Substring(6, 2)) + 1900;// 截取变量字符串       
            }
            else if (id.Length == 18)
            {
                if (!this.CheckCardId(id))
                {
                    MessageBox.Show("您输入的身份证号码有误！");
                    return;
                }

                year = Convert.ToInt32(id.Substring(6, 4));
                
            }
            else
            {
                MessageBox.Show("身份证号码长度有误，请重新输入！");
                return;
            }


            age = DateTime.Now.Year - year;
            if (age < 18)
            {
                MessageBox.Show("你太小了，回家看动画片吧!");
                return;
            }
            else
            {
                picboxTuPian.Visible = true;
            }
        }
    }
}
